
clear all
cd "/Users/jzingher/Dropbox/Education Realignment/Data/Analysis Files"
use "ANES Adjusted", clear

/// Religious Groups
*Mainline Protestants
gen Mainline =0 
replace Mainline=1 if VCF0152==110
replace Mainline=1 if VCF0152==121
replace Mainline=1 if VCF0152==150
replace Mainline=1 if VCF0152==160
replace Mainline=1 if VCF0152==165
replace Mainline=1 if VCF0152==220
replace Mainline=1 if VCF0152==230
replace Mainline=1 if VCF0152==270
replace Mainline=1 if VCF0152==281
replace Mainline=1 if VCF0152==290
replace Mainline=1 if VCF0152==292 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==010 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==099 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==199 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==149 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==229 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==249 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==279 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==289 & race_4cat~=2 & VCF9243==2
replace Mainline=1 if VCF0152==292 & race_4cat~=2 & VCF9243==2
*Traditionally Black Denominations
gen B_Protestant =0
replace B_Protestant=1 if VCF0152==010 & race_4cat==2
replace B_Protestant=1 if VCF0152==099 & race_4cat==2
replace B_Protestant=1 if VCF0152==129
replace B_Protestant=1 if VCF0152==130
replace B_Protestant=1 if VCF0152==131
replace B_Protestant=1 if VCF0152==132
replace B_Protestant=1 if VCF0152==133
replace B_Protestant=1 if VCF0152==149 & race_4cat==2
replace B_Protestant=1 if VCF0152==229 & race_4cat==2
replace B_Protestant=1 if VCF0152==249 & race_4cat==2
replace B_Protestant=1 if VCF0152==199 & race_4cat==2
replace B_Protestant=1 if VCF0152==231
replace B_Protestant=1 if VCF0152==232
replace B_Protestant=1 if VCF0152==233
replace B_Protestant=1 if VCF0152==257
replace B_Protestant=1 if VCF0152==279 & race_4cat==2
replace B_Protestant=1 if VCF0152==289 & race_4cat==2
replace B_Protestant=1 if VCF0152==292 & race_4cat==2
* Evangelical
gen Evangelical =0
replace Evangelical=1 if VCF0152==010 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==099 & race_4cat~=2 & VCF9243==1
replace Evangelical =1 if VCF0152==100 
replace Evangelical =1 if VCF0152==109 
replace Evangelical =1 if VCF0152==120
replace Evangelical =1 if VCF0152==122
replace Evangelical =1 if VCF0152==123
replace Evangelical =1 if VCF0152==124
replace Evangelical =1 if VCF0152==125  
replace Evangelical =1 if VCF0152==126  
replace Evangelical =1 if VCF0152==127
replace Evangelical =1 if VCF0152==128  
replace Evangelical =1 if VCF0152==135 
replace Evangelical =1 if VCF0152==149 & race_4cat~=2 & VCF9243==1
replace Evangelical =1 if VCF0152==162
replace Evangelical =1 if VCF0152==166
replace Evangelical =1 if VCF0152==167
replace Evangelical =1 if VCF0152==168
replace Evangelical =1 if VCF0152==169
replace Evangelical =1 if VCF0152==180
replace Evangelical =1 if VCF0152==181 
replace Evangelical =1 if VCF0152==182 
replace Evangelical =1 if VCF0152==183 
replace Evangelical =1 if VCF0152==184 
replace Evangelical =1 if VCF0152==185 
replace Evangelical =1 if VCF0152==200
replace Evangelical =1 if VCF0152==221
replace Evangelical =1 if VCF0152==224
replace Evangelical =1 if VCF0152==225
replace Evangelical =1 if VCF0152==242
replace Evangelical =1 if VCF0152==243
replace Evangelical =1 if VCF0152==244
replace Evangelical =1 if VCF0152==250
replace Evangelical =1 if VCF0152==251
replace Evangelical =1 if VCF0152==253
replace Evangelical =1 if VCF0152==255
replace Evangelical =1 if VCF0152==256
replace Evangelical =1 if VCF0152==258
replace Evangelical =1 if VCF0152==260
replace Evangelical =1 if VCF0152==261
replace Evangelical =1 if VCF0152==262
replace Evangelical =1 if VCF0152==264
replace Evangelical =1 if VCF0152==267
replace Evangelical =1 if VCF0152==271
replace Evangelical =1 if VCF0152==272
replace Evangelical =1 if VCF0152==275
replace Evangelical =1 if VCF0152==280
replace Evangelical=1 if VCF0152==149 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==199 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==229 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==249 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==279 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==289 & race_4cat~=2 & VCF9243==1
replace Evangelical=1 if VCF0152==292 & race_4cat~=2 & VCF9243==1

/// Weekly Church Attendee

replace religion=. if religion==0
/// Protestant
gen Protestant =0
replace Protestant=1 if B_Protestant==1 | Mainline==1
/// recode unionmember
recode Union (2=0)
/// Feeling Therm

rename VCF0227 FT_Asian

gen outaveblack = (FT_Latinos+FT_Whites+FT_Asian)/3
gen outavewhite = (FT_Latinos+FT_Blacks+FT_Asian)/3
gen outaveLatino = (FT_Blacks+FT_Whites+FT_Asian)/3
gen outaveAsian = (FT_Latinos+FT_Whites+FT_Blacks)/3



gen Asian=0
replace Asian =1 if race_4cat==4
replace race_4cat=. if race_4cat==9
/// College Graduate
replace collegegrad = 1 if education_6cat==6
replace education_7 =. if education_7 >=8
/// Race
label define Race 1 "White" 2 "Black" 3 "Latino" 4 "Asian" 
label values race_4cat Race

/// Self identified Social Class
rename VCF0148 Class_SI
replace Class_SI =. if Class_SI==9
replace VCF0115=. if VCF0115==0
replace religion=. if white~=1 & religion==3


gen BA=0
replace BA=1 if education_7cat>=6

asdoc regress PID_7 gender age i.race_4cat Union Income i.religion i.year##i.BA [aw= VCF0010z], replace nested dec(2) save(ANES_PID)
margins, dydx(BA) at(year=(1976(4)2020)) predict(xb) 
marginsplot, recast(scatter) horizontal title(ANES Full Sample) ytitle(Year) xtitle(Effect of BA on PID) xline(0) xlabel(-1(.5)1)
graph save "PID All ANES", replace 
graph export "PID All ANES.pdf", replace 

asdoc regress PID_7 gender age Union Income i.religion i.year##i.BA [aw= VCF0010z] if race_4cat==1, append nested dec(2) save(ANES_PID)
margins, dydx(BA) at(year=(1976(4)2020)) predict(xb) 
marginsplot, recast(scatter) horizontal title(ANES Whites Only) ytitle(Year) xtitle(Effect of BA on PID) xline(0)
graph save "PID Whites ANES", replace 
graph export "PID Whites ANES.pdf", replace 

asdoc logit Pres_Vote PID_7 gender age i.race_4cat Union Income i.religion i.year##i.BA [pw= VCF0010z], replace nested dec(2) save(ANES_VC)
margins, dydx(BA) at(year=(1976(4)2020)) 
marginsplot, recast(scatter) horizontal title(ANES Full Sample) ytitle(Year) xtitle(Effect of BA on Dem Vote Choice) xline(0)
graph save "PV All ANES", replace 
graph export "PV All ANES.pdf", replace 

asdoc logit Pres_Vote PID_7 gender age Union Income i.religion i.year##i.BA [pw= VCF0010z] if race_4cat==1, append nested dec(2) save(ANES_VC)
margins, dydx(BA) at(year=(1976(4)2020))  
marginsplot, recast(scatter) horizontal title(ANES Whites Only) ytitle(Year) xtitle(Effect of BA on Dem Vote Choice) xline(0)
graph save "PV Whites ANES", replace 
graph export "PV Whites ANES.pdf", replace 

* Racial Resentment Index Setup
rename VCF9040 blacks_favors
rename VCF9039 blacks_slavery
rename VCF9042 blacks_deserve
rename VCF9041 blacks_work
replace blacks_favors = . if blacks_favors>5
replace blacks_slavery = . if blacks_slavery>5
replace blacks_deserve = . if blacks_deserve>5
replace blacks_work = . if blacks_work>5

recode blacks_favors (5=1) (4=2) (2=4) (1=5)
recode blacks_work (5=1) (4=2) (2=4) (1=5)

polychoric blacks_favors blacks_slavery blacks_deserve blacks_work [pweight=VCF0010z], pw
display r(sum_w)                         
matrix polyRR = r(R) 
factormat polyRR, n(19251) factors(1)  ml
predict RR 
zscore blacks_favors blacks_slavery blacks_deserve blacks_work
alpha blacks_favors blacks_slavery blacks_deserve blacks_work


gen I1= z_blacks_favors*-.294
gen I2= z_blacks_slavery*.25
gen I3= z_blacks_deserve*.306
gen I4= z_blacks_work*-.248

egen I1_max1=  max(I1)   
egen I2_max2=  max(I2)    
egen I3_max3=  max(I3)
egen I4_max4=  max(I4)

gen I1_MI = 1 if I1~=.
gen I2_MI = 1 if I2~=.
gen I3_MI = 1 if I3~=.
gen I4_MI = 1 if I4~=.

gen I1_Ind_MAX = I1_max1* I1_MI
gen I2_Ind_MAX = I2_max2* I2_MI
gen I3_Ind_MAX = I3_max3* I3_MI
gen I4_Ind_MAX = I4_max4* I4_MI


egen RR_Ind_Max = rowtotal(I1_Ind_MAX-I4_Ind_MAX)
egen MaxRR = max(RR_Ind_Max)
gen Pct_MaxRR = RR_Ind_Max/MaxRR

egen RR_Score = rowtotal(I1-I4)
replace RR_Score=. if Pct_MaxRR<.6
replace RR_Score= RR_Score/Pct_MaxRR if RR_Score~=.


replace VCF9246=. if VCF9246<0
replace VCF9247=. if VCF9247<0
replace VCF9248=. if VCF9248<0
replace VCF9249=. if VCF9249<0


* Authoritarianism Index Setup 
polychoric VCF9246 VCF9247 VCF9248 VCF9249 [pweight=VCF0010z], pw
display r(sum_w)                         
matrix polyAUT = r(R) 
factormat polyAUT, n(11457) factors(1)  ml
predict AUT 
zscore VCF9246 VCF9247 VCF9248 VCF9249
alpha VCF9246 VCF9247 VCF9248 VCF9249


gen AUT1= z_VCF9246*.386
gen AUT2= z_VCF9247*-.33
gen AUT3= z_VCF9248*.145
gen AUT4= z_VCF9249*.256

egen AUT1_max1=  max(AUT1)   
egen AUT2_max2=  max(AUT2)    
egen AUT3_max3=  max(AUT3)
egen AUT4_max4=  max(AUT4)

gen AUT1_MI = 1 if AUT1~=.
gen AUT2_MI = 1 if AUT2~=.
gen AUT3_MI = 1 if AUT3~=.
gen AUT4_MI = 1 if AUT4~=.

gen AUT1_Ind_MAX = AUT1_max1* AUT1_MI
gen AUT2_Ind_MAX = AUT2_max2* AUT2_MI
gen AUT3_Ind_MAX = AUT3_max3* AUT3_MI
gen AUT4_Ind_MAX = AUT4_max4* AUT4_MI

egen AUT_Ind_Max = rowtotal(AUT1_Ind_MAX-AUT4_Ind_MAX)
egen MaxAUT = max(AUT_Ind_Max)
gen Pct_MaxAUT = AUT_Ind_Max/MaxAUT

egen AUT_Score = rowtotal(AUT1-AUT4)
replace AUT_Score=. if Pct_MaxAUT<.6
replace AUT_Score= AUT_Score/Pct_MaxAUT if RR_Score~=.


* Mediation Analysis
medeff (regress PID_7 gender age Union Income BA Catholic Latino Black   ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic  Latino Black ) [pw= VCF0010z] if year==1972, mediate(PID_7) treat(BA) 
medeff (regress PID_7 gender age Union Income BA Catholic Latino Black   ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic  Latino Black ) [pw= VCF0010z] if year==1976, mediate(PID_7) treat(BA) 
medeff (regress PID_7 gender age Union Income BA Catholic Latino Black   ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic  Latino Black ) [pw= VCF0010z] if year==1980, mediate(PID_7) treat(BA) 
medeff (regress PID_7 gender age Union Income BA Catholic Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Latino Black Asian) [pw= VCF0010z] if year==1984, mediate(PID_7) treat(BA)
medeff (regress PID_7 gender age Union Income BA Catholic Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Latino Black Asian) [pw= VCF0010z] if year==1988, mediate(PID_7) treat(BA)
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==1992, mediate(PID_7) treat(BA)
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==1996, mediate(PID_7) treat(BA)
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==2000, mediate(PID_7) treat(BA)
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==2004, mediate(PID_7) treat(BA)  
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==2008, mediate(PID_7) treat(BA) 
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==2012, mediate(PID_7) treat(BA) 
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==2016, mediate(PID_7) treat(BA)  
medeff (regress PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian  ) (logit Pres_Vote PID_7 gender age Union Income BA Catholic Mainline Evangelical B_Protestant Latino Black Asian) [pw= VCF0010z] if year==2020, mediate(PID_7) treat(BA)  



* Potential Education Pathways 

asdoc regress PID_7 gender age i.race_4cat Union Income i.religion i.year##i.BA RR_Score AUT_Score Immigrants_Scale F2_Adj F1_Adj  i.year##i.BA[pw= VCF0010z], save(ANES_PID2) replace  nested dec(2) 
margins, dydx(BA) at(year=(2000(4)2020)) predict(xb) 
marginsplot, recast(scatter) horizontal title(ANES Full Sample) ytitle(Year) xtitle(Effect of BA on PID) xline(0)
* Potential Education Pathways, Whites only

asdoc regress PID_7 gender age Union Income i.religion i.year##i.BA RR_Score AUT_Score Immigrants_Scale F2_Adj F1_Adj i.year##i.BA [pw= VCF0010z] if race_4cat==1, save(ANES_PID2) append nested dec(2) 
margins, dydx(BA) at(year=(2000(4)2020)) predict(xb) 
marginsplot, recast(scatter) horizontal title(ANES Whites Only) ytitle(Year) xtitle(Effect of BA on PID) xline(0)

* Potential Education Pathways 
asdoc logit Pres_Vote PID_7 gender age  i.race_4cat  Union Income i.religion i.year##i.BA RR_Score AUT_Score Immigrants_Scale F2_Adj F1_Adj i.year##i.BA [pw= VCF0010z], save(ANES_VC2) replace  nested dec(2)  
margins, dydx(BA) at(year=(2000(4)2020))
marginsplot, recast(scatter) horizontal title(ANES Full Sample) ytitle(Year) xtitle(Effect of BA on Vote Choice) xline(0)
* Potential Education Pathways, Whites only

asdoc logit Pres_Vote PID_7 gender age Union Income i.religion i.year##i.BA RR_Score AUT_Score Immigrants_Scale F2_Adj F1_Adj i.year##i.BA [pw= VCF0010z] if race_4cat==1, save(ANES_VC2) append  nested dec(2)  
margins, dydx(BA) at(year=(2000(4)2020)) 
marginsplot, recast(scatter) horizontal title(ANES Whites Only) ytitle(Year) xtitle(Effect of BA on Vote Choice) xline(0)
* Table 1 Difference of Means
asdoc ttest F1_Adj if race_4cat==1, by(BA) replace 
asdoc ttest F2_Adj if race_4cat==1, by(BA) rowappend 
asdoc ttest RR_Score if race_4cat==1, by(BA) rowappend
asdoc ttest AUT_Score if race_4cat==1, by(BA) rowappend 
asdoc ttest Immigrants_Scale if race_4cat==1, by(BA) rowappend 
